/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wwdablu.soumya.lottiebottomnav.util;

import ohos.agp.components.AttrSet;
import ohos.agp.components.element.Element;
import ohos.agp.utils.Color;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

import java.util.Optional;

/**
 * 自定义属性读取
 *
 * @since 2021-04-21
 */
public final class AttrUtil {
    private static final String KEY = ":";
    private static final String NUMBERFORMATEXCEPTIONS = "NumberFormatException";
    private static final String ILLEGALSTATEEXCEPTIONS = "IllegalStateException";

    private AttrUtil() {
    }

    /**
     * String 属性读取
     *
     * @param attrSet attrSet
     * @param key key
     * @param defValue defValue
     * @return String
     */
    public static String getStringValue(AttrSet attrSet, String key, String defValue) {
        if (attrSet.getAttr(key).isPresent()) {
            return attrSet.getAttr(key).get().getStringValue();
        } else {
            return defValue;
        }
    }

    /**
     * Color颜色值读取
     *
     * @param attrSet attrSet
     * @param key key
     * @param defValue defValue
     * @return Color
     */
    public static Color getColorValue(AttrSet attrSet, String key, String defValue) {
        if (attrSet.getAttr(key).isPresent()) {
            return attrSet.getAttr(key).get().getColorValue();
        } else {
            return new Color(Color.getIntColor(defValue));
        }
    }

    /**
     * Integer值读取
     *
     * @param attrSet attrSet
     * @param key key
     * @param defValue defValue
     * @return Integer
     */
    public static int getColorInteger(AttrSet attrSet, String key, String defValue) {
        if (attrSet.getAttr(key).isPresent()) {
            return Color.getIntColor(attrSet.getAttr(key).get().getStringValue());
        } else {
            return Color.getIntColor(defValue);
        }
    }

    /**
     * getBooleanValue 值读取
     *
     * @param attrSet AttrSet
     * @param key String
     * @param isDefValue boolean
     * @return boolean
     */
    public static boolean getBooleanValue(AttrSet attrSet, String key, boolean isDefValue) {
        if (attrSet.getAttr(key).isPresent()) {
            return attrSet.getAttr(key).get().getBoolValue();
        } else {
            return isDefValue;
        }
    }

    /**
     * Element读取
     *
     * @param attrSet AttrSet
     * @param key String
     * @param defValue Element
     * @return Element 资源
     */
    public static Element getElementValue(AttrSet attrSet, String key, Element defValue) {
        if (attrSet.getAttr(key).isPresent()) {
            return attrSet.getAttr(key).get().getElement();
        } else {
            return defValue;
        }
    }

    /**
     * Dimension读取
     *
     * @param attrSet AttrSet
     * @param key String
     * @param defDimensionValue int
     * @return int Dimension返回
     */
    public static int getDimension(AttrSet attrSet, String key, int defDimensionValue) {
        if (attrSet.getAttr(key).isPresent()) {
            return attrSet.getAttr(key).get().getDimensionValue();
        } else {
            return defDimensionValue;
        }
    }

    /**
     * Integer属性读取
     *
     * @param attrSet AttrSet
     * @param key String
     * @param defValue Integer
     * @return int Integer属性读取
     */
    public static int getIntegerValue(AttrSet attrSet, String key, int defValue) {
        if (attrSet.getAttr(key).isPresent()) {
            return attrSet.getAttr(key).get().getIntegerValue();
        } else {
            return defValue;
        }
    }

    /**
     * 获取资源文件id 格式 $layout:布局名称
     *
     * @param attrSet attrSet
     * @param resourceKey resourceKey
     * @param defvalue defvalue
     * @return int
     */
    public static int getResourceId(AttrSet attrSet, String resourceKey, int defvalue) {
        int resourceId = defvalue;
        if (attrSet.getAttr(resourceKey).isPresent()) {
            String value = attrSet.getAttr(resourceKey).get().getStringValue();
            if (value.contains(KEY)) {
                try {
                    resourceId = Integer.parseInt(value.substring(value.indexOf(KEY) + 1));
                } catch (NumberFormatException e) {
                    LogUtil.error(NUMBERFORMATEXCEPTIONS, "getResourceId err");
                }
            } else {
                LogUtil.error(ILLEGALSTATEEXCEPTIONS, "err");
            }
        }
        return resourceId;
    }

    /**
     * 获取资源文件id 格式 $layout:布局名称
     *
     * @param attrSet attrSet
     * @param resourceKey resourceKey
     * @param defvalue defvalue
     * @return id
     */
    public static int getMediaId(AttrSet attrSet, String resourceKey, int defvalue) {
        int resourceId = defvalue;
        if (attrSet.getAttr(resourceKey).isPresent()) {
            String value = attrSet.getAttr(resourceKey).get().getStringValue();
            if (value.contains(KEY)) {
                try {
                    resourceId = Integer.parseInt(value.substring(value.indexOf(':') + 1));
                } catch (NumberFormatException e) {
                    LogUtil.error(NUMBERFORMATEXCEPTIONS, NUMBERFORMATEXCEPTIONS);
                }
            } else {
                LogUtil.error(ILLEGALSTATEEXCEPTIONS, ILLEGALSTATEEXCEPTIONS);
            }
        }
        return resourceId;
    }

    /**
     * dip2px转换
     *
     * @param context context
     * @param dpValue dpValue
     * @return float
     */
    public static float dip2px(Context context, float dpValue) {
        DisplayAttributes displayAttributes = getScreenPiex(context);
        return dpValue * displayAttributes.scalDensity;
    }

    /**
     * sp2px转换
     *
     * @param context context
     * @param spValue spValue
     * @return float
     */
    public static float sp2px(Context context, float spValue) {
        DisplayAttributes displayAttributes = getScreenPiex(context);
        return spValue * displayAttributes.densityPixels;
    }

    /**
     * 获取屏幕密度
     *
     * @param context context
     * @return DisplayAttributes
     */
    public static DisplayAttributes getScreenPiex(Context context) {
        Optional<Display> display = DisplayManager.getInstance().getDefaultDisplay(context);
        DisplayAttributes displayAttributes = null;
        if (display.isPresent()) {
            displayAttributes = display.get().getAttributes();
        }
        return displayAttributes;
    }
}
